package com.cloud.manager.utils.parser;

import com.cloud.manager.utils.FileSizeUtil;
import com.cloud.manager.web.domain.App;
import com.cloud.manager.web.domain.AppInfo;
import com.cloud.manager.web.domain.Version;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.IconFace;
import org.apache.commons.io.FileUtils;

import java.io.File;

/**
 * 描述：
 *
 * @author FineEx   2021/12/1
 * @version 1.0
 */
public class APKParser implements AppParser {

    @Override
    public AppInfo parse(String filePath) {
        try {
            AppInfo appInfo = new AppInfo();

            File file = new File(filePath);
            if (!file.exists()) return null;
            ApkFile apkFile = new ApkFile(file);
            App app = new App();
            ApkMeta meta = apkFile.getApkMeta();
            app.setAppName(meta.getName());
            String versionName = meta.getVersionName();
            long buildVersion = meta.getVersionCode();
            if (versionName.length() < 1) {
                versionName = meta.getPlatformBuildVersionName();
            }
            app.setVersionCode((int) buildVersion);
            app.setVersionName(versionName);
            app.setPackageName(meta.getPackageName());
            app.setPlatform("android");
            app.setState(true);


            Version version = new Version();
            version.setVersionCode((int) buildVersion);
            version.setVersionName(versionName);
            version.setState(true);
            version.setSize((int) FileSizeUtil.getFileOrFilesSize(file.length() + "", FileSizeUtil.SIZE_TYPE_MB));
            apkFile.close();

            appInfo.setApp(app);
            appInfo.setVersion(version);
            return appInfo;
        } catch (Exception e) {

        }
        return null;
    }
}
